<?php

namespace System\Lib;

/**
 * Description of Parser
 *
 * @author elton.schivei
 */
class Parser extends \System\Types\Object
{

    private $l_delim = '{';
    private $r_delim = '}';

    /**
     * @var \System\Core\View
     */
    private $_view;

    function __construct(\System\Core\View $view)
    {
        parent::__construct();

        $this->_view = $view;
    }

    /**
     *  Parse a template
     *
     * Parses pseudo-variables contained in the specified template view,
     * replacing them with the data in the second param
     *
     * @access	public
     * @param	array
     * @param	\System\Core\View
     * @param	bool
     * @return	string
     */
    public function Parse($data, \System\Core\View $view = null, $return = FALSE)
    {
        $view = $view ? : $this->_view;

        /* @var $loader \System\Core\Loader */
        $loader = \System\Core\Loader::GetInstance();

        $filename = $view->GetType()->getFileName();

        $filename = \str_replace('.php', '-design.php', $filename);

        $template = $loader->LoadFileContent($filename);

        return $this->_parse($template, $data, $return);
    }

    // --------------------------------------------------------------------

    /**
     *  Parse a String
     *
     * Parses pseudo-variables contained in the specified string,
     * replacing them with the data in the second param
     *
     * @access	public
     * @param	string
     * @param	array
     * @param	bool
     * @return	string
     */
    public function ParseString($template, $data, $return = FALSE)
    {
        return $this->_parse($template, $data, $return);
    }

    // --------------------------------------------------------------------

    /**
     *  Parse a template
     *
     * Parses pseudo-variables contained in the specified template,
     * replacing them with the data in the second param
     *
     * @access	public
     * @param	string
     * @param	array
     * @param	bool
     * @return	string
     */
    private function _parse($template, $data, $return = TRUE)
    {
        if ($template == '')
        {
            return FALSE;
        }

        foreach ($data as $key => $val)
        {
            if (\is_array($val))
            {
                $template = $this->_parse_pair($key, $val, $template);
            }
            else
            {
                $template = $this->_parse_single($key, (string) $val, $template);
            }
        }

        if ($return == FALSE)
        {
            /* @var $out \System\Core\Output */
            $out = \System\Core\Output::GetInstance();
            $out->AppendOutput($template);
        }

        return $template;
    }

    // --------------------------------------------------------------------

    /**
     *  Set the left/right variable delimiters
     *
     * @access	public
     * @param	string
     * @param	string
     * @return	void
     */
    public function SetDelimiters($l = '{', $r = '}')
    {
        $this->l_delim = $l;
        $this->r_delim = $r;
    }

    public function ResetDelimiters()
    {
        $this->SetDelimiters();
    }

    // --------------------------------------------------------------------

    /**
     *  Parse a single key/value
     *
     * @access	private
     * @param	string
     * @param	string
     * @param	string
     * @return	string
     */
    private function _parse_single($key, $val, $string)
    {
        return \str_replace($this->l_delim . $key . $this->r_delim, $val, $string);
    }

    // --------------------------------------------------------------------

    /**
     *  Parse a tag pair
     *
     * Parses tag pairs:  {some_tag} string... {/some_tag}
     *
     * @access	private
     * @param	string
     * @param	array
     * @param	string
     * @return	string
     */
    private function _parse_pair($variable, $data, $string)
    {
        $match = $this->_match_pair($string, $variable);

        if (FALSE === $match)
        {
            return $string;
        }

        $str = '';
        foreach ($data as $row)
        {
            $temp = $match['1'];
            foreach ($row as $key => $val)
            {
                if (!\is_array($val))
                {
                    $temp = $this->_parse_single($key, $val, $temp);
                }
                else
                {
                    $temp = $this->_parse_pair($key, $val, $temp);
                }
            }

            $str .= $temp;
        }

        return \str_replace($match['0'], $str, $string);
    }

    // --------------------------------------------------------------------

    /**
     *  Matches a variable pair
     *
     * @access	private
     * @param	string
     * @param	string
     * @return	mixed
     */
    private function _match_pair($string, $variable)
    {
        if (!\preg_match("|" .
                        \preg_quote($this->l_delim) . $variable .
                        \preg_quote($this->r_delim) . "(.+?)" .
                        \preg_quote($this->l_delim) . '/' . $variable .
                        \preg_quote($this->r_delim) . "|s", $string, $match))
        {
            return FALSE;
        }

        return $match;
    }

}
